public class Util {

	@future(callout=true)
	public static void createToDos(List<String> ids) {
		List<ToDo__c> todos = [Select Id, Name, AssignedTo__c, DueDate__c, Meeting_Report__r.ActivityId__c From ToDo__c Where Id in :ids];
		String protocol = 'https'; 
		String username = 'davecarroll@mailinator.com';
		String password = 'Hailey24';
		String activitiesServer = 'bluehouseisv.lotus.com';
		String userId = 'davecarroll@mailinator.com';
		String activityTitle = 'testing123';
 
        AtomService activitiesService = new AtomService(protocol, activitiesServer, username, password);
		ActivitiesClient activitiesClient = new ActivitiesClient(activitiesService);
		
		for (ToDo__c todo : todos) {
			Entry todoItem = new Entry();
			todoItem.setTitle(todo.Name);
			todoItem = activitiesClient.createToDoItem(todoItem, todo.Meeting_Report__r.ActivityId__c);
			
		}
		
	}
    //private static String appId = 'test_app';
    //private static String password = '87323at4aj6y8e9a0pa92w';
    //private static String appId = 'salesforce';
    //private static String password = 'm97z190cxs300l9mt8d987b';
    private static String appId = 'dcarroll@salesforce.com';
    private static String password = 'Hailey24';
    private static String bhUrl = 'https://bluehouse.lotus.com/';
    private static String fileUrl { get { return bhUrl + 'files/atom/'; } }
    private static String userOfInterest = 'dcarroll@salesforce.com';
    private static String activityUrl = 'activities/service/atom2/';
    private static String serviceUrl { get { return bhUrl + activityUrl + 'service'; } }
    
    public static List<Entry> files;
    public static List<Entry> collections;
    public static List<Entry> filesAndCollections;
    
    public static HttpResponse makeRESTPost(HttpRequest req) {
        Blob headerValue = Blob.valueOf(appId + ':' + password);
        String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
        req.setHeader('Authorization', authorizationHeader);
		req.setMethod('POST');
    	Http h = new Http(); 
    	return h.send(req);
    }
     
    public static AtomBase makeRESTCall(String path, Boolean showRawFeed) {
        HTTP h = new HTTP();
        HTTPRequest r = new HTTPRequest();
        
        //r.setEndpoint(baseUrl + userOfInterest + '/' + operation + '?onBehalfOf=' + userOfInterest);
        r.setEndpoint(path );
        System.debug(LoggingLevel.INFO, '\n\nURL: ' + r.getEndpoint() + '\n\n');
        
        Blob headerValue = Blob.valueOf(appId + ':' + password);
        String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
        r.setHeader('Authorization', authorizationHeader);
        
        r.setMethod('GET');
        HTTPResponse resp = h.send(r);
        if (resp.getStatusCode() != 200) {
            System.debug(LoggingLevel.INFO, resp.getStatusCode() + '\n' + resp.getStatus());
            return null;
        } else {
            if (showRawFeed) {
                System.debug(LoggingLevel.INFO, resp.getBody());
            }
            AtomParser dom = new AtomParser(resp.getBody());
            System.debug('\n\n' + dom + '\n');
            return dom.root;
           // return dom.ownerDocument().firstChild();
        }
    }
    public static Feed makeFileCall(String path, Boolean showRawFeed) {
        return (Feed)makeRESTCall(path, showRawFeed);
    } 
    
    public static AtomBase makeServiceCall(String path, Boolean showRawFeed) {
        return makeRESTCall(path, showRawFeed);
    }
    public static void createActivity() {
    	HttpRequest req = new HttpRequest();
    	req.setEndpoint('https://bluehouseisv.lotus.com/activities/service/atom/activities');
    	req.setMethod('POST');
    	req.setBody('<entry xmlns="http://www.w3.org/2005/Atom"><title type="text">testing123</title></entry>');
    	req.setHeader('Content-Type', 'application/atom+xml');
    	HttpResponse resp = makeRESTPost(req);
    	System.debug(LoggingLevel.INFO, '\n\nReponse is: ' + resp);
    	System.debug(LoggingLevel.INFO, '\n\nResponse Body: ' + resp.getBody());
    	
    }
    public static void TestSuite() {
        /*filesAndCollections = getFilesAndCollections();
        files = getFiles();
        collections = getCollections();
        String[] fileIdField = files[0].id.split(':');
        String fileId = fileIdField[fileIdField.size() - 1];
        System.debug(LoggingLevel.INFO, '\n\nThe file id is: ' + fileId + '\n\n');*/
        String testId = '4A8C4C50CB0011DDB2BAE4EC0921069C';
        Entry fe = getFileEntry(testId);
        if (fe == null) {
            System.debug(LoggingLevel.INFO, '\n\nFile entry resulted in null.\n\n');
        } else {
            System.debug(LoggingLevel.INFO, '\n\nFile entry found, ' + fe.title + '\n\n');
        }
        /*getFileMedia();
        getCollectionEntry();
        getCollectionFeed();
        getFileShares();
        getCollectionShares();
        getFileShareDetails();
        getCollectionShareDetails();
        getSharedItemsFromUserToUser();

        postFileOrCollection();
        postFileShare();
        postCollectionShare();
        
        deleteFileShare();
        deleteCollectionShare();*/
                
    }
    
    
    public static List<Feed> getActivityDetails() {
        BluehouseService actserv = getActivityService();
        List<Feed> activityFeeds = new List<Feed>();
        /*for (String key : actserv.activitiesWorkspace.collections.keySet()) {
            ServiceCollection sc = actserv.activitiesWorkspace.collections.get(key);
            VirElement result = makeRESTCall(sc.href + '?onBehalfOf=' + userOfInterest, true);
            if (result instanceof Feed) {
            	System.debug(LoggingLevel.INFO, '\n\nSays here it is a feed...\n\n');
            } else {
            	System.debug(LoggingLevel.INFO, '\n\nSays here it is NOT a feed...\n\n');
            }
            //System.debug(LoggingLevel.INFO, '\n\nHere are the activity details:\n\n' + makeRESTCall(sc.href + '?onBehalfOf=' + userOfInterest, true));
        }*/
        return activityFeeds;
    }
    public static void getEverything() {
    	//makeRESTCall('https://bluehouseisv.lotus.com/activities/service/atom2/everything?onBehalfOf=' + userOfInterest, true);
    	AtomBase obj = makeRESTCall('https://bluehouseisv.lotus.com/activities/service/atom2/todos?onBehalfOf=' + userOfInterest, true);
    	System.debug(LoggingLevel.INFO, '\n\nOBJ: \n' + obj);
    	Feed feed = (Feed)obj.getChild();
    	List<Entry> entries = feed.getEntries();
    	for (Entry e : entries) {
    		System.debug(LoggingLevel.INFO, '\n\n' + e.extensions);
    		ToDoEntry tde = (ToDoEntry)e.clone();
    		tde.convertToToDo((ActivityEntry)e);
    		for (String key : e.extensions.keySet()) {
    			Element el = e.extensions.get(key);
    			System.debug(LoggingLevel.WARN, '\nELEMENT: ' + el.name + ' \n' + el);
    		}
    	} 
    }
    
    public static BluehouseService getActivityService() {
    	BluehouseService bhs = (BluehouseService)makeServiceCall(serviceUrl + '?onBehalfOf=' + userOfInterest, true);
        System.debug('\n\nNumber of workspaces: ' + bhs.workspaces.size());
        System.debug('\n\nGenerator: ' + bhs.generator + '\n');
        String dOut = '';
        for (Workspace w : bhs.workspaces) {
        	dOut += w.title + '\n';
        	for (ServiceCollection c : w.collection) {
        		dOut += '\tCollection: ' + c.title + '\n';
        		for (Categories cat : c.categories) {
        			dOut += '\t\tCategory: ' + cat.href + '\n';
        		}
        	}
        }
        System.debug('\n\n' + dOut);
        return bhs;
    }
    public static Entry getFileEntry(String fileId) {
        Feed feed = makeFileCall(fileUrl + 'file/' + fileId + '?onBehalfOf=' + userOfInterest, true);
        Entry fEntry = feed.entry[0];
        if (fEntry != null) {
            return fEntry;
        } else {
            return null;
        }
    }
    
    public static List<Entry> getFilesandCollections() {
        List<Entry> fileEntries = new List<Entry>();
        Feed feed = makeFileCall(fileUrl + userOfInterest + '?onBehalfOf=' + userOfInterest, false);
        if (feed != null) {
            for (Entry entry : feed.entry) {
                fileEntries.add(entry);
            }
        }
        return fileEntries;
    }
    public static List<Entry> getFiles() {
        List<Entry> fileEntries = new List<Entry>();
        Feed feed = makeFileCall(fileUrl + userOfInterest + '/files?onBehalfOf=' + userOfInterest, false);
        if (feed != null) {
            for (Entry entry : feed.entry) {
                fileEntries.add(entry);
            }
        }
        return fileEntries;
    }
    public static List<Entry> getCollections() {
        List<Entry> fileEntries = new List<Entry>();
        Feed feed = makeFileCall(fileUrl + userOfInterest + '/collections?onBehalfOf=' + userOfInterest, false);
        if (feed != null) {
            for (Entry entry : feed.entry) {
                fileEntries.add(entry);
            }
        }
        return fileEntries;
    }
    //public static void 
}